package com.dongdong.bookstore.module.ums.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.bookstore.common.api.CommonPage;
import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.Resource;
import com.dongdong.bookstore.module.ums.service.ResourceService;
import com.dongdong.bookstore.module.ums.service.UmsCacheService;
import com.dongdong.bookstore.module.ums.viewmodel.ResourceParam;
import com.dongdong.bookstore.security.components.DynamicSecurityMetadataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台资源  前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@Api(tags = "后台权限资源管理")
@RestController
@RequestMapping("/ums/resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

    @Autowired
    private UmsCacheService umsCacheService;

    @ApiOperation("添加后台资源")
    @PostMapping("/create")
    public String create(@RequestBody ResourceParam resourceParam) {
        Resource target = new Resource();
        BeanUtils.copyProperties(resourceParam, target);
        Asserts.isFalse(resourceService.save(target), "添加资源失败");
        return "添加成功";
    }

    @ApiOperation("更新后台资源")
    @PostMapping("/update/{id}")
    public String update(@PathVariable Long id, @RequestBody ResourceParam resourceParam) {
        Resource source = resourceService.getById(id);
        Asserts.isNull(source, "更新的资源不存在");
        BeanUtils.copyProperties(resourceParam, source);
        
        Asserts.isFalse(resourceService.updateById(source), "资源更新失败");
        umsCacheService.clearResourcesListAll();
        dynamicSecurityMetadataSource.clearDataSource();
        return "更新资源成功";
    }

    @ApiOperation("根据ID获取资源信息")
    @PostMapping("/{id}")
    public Resource getResource(@PathVariable Long id) {
        Resource resource = resourceService.getById(id);
        Asserts.isNull(resource, "资源不存在");
        return resource;
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        Resource resource = resourceService.getById(id);
        Asserts.isNull(resource, "资源不存在，删除失败");
        Asserts.isFalse(resourceService.removeById(id), "删除失败,请刷新再试");
        umsCacheService.clearResourcesListAll();
        dynamicSecurityMetadataSource.clearDataSource();
        return "删除成功";
    }

    @ApiOperation("分页模糊查询后台资源")
    @GetMapping("/list")
    public CommonPage<Resource> list(@RequestParam(required = false) Long categoryId,
                                     @RequestParam(required = false) String nameKeyword,
                                     @RequestParam(required = false) String urlKeyword,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<Resource> resourceList = resourceService.list(categoryId, nameKeyword, urlKeyword, pageSize, pageNum);
        return CommonPage.restPage(resourceList);
    }

    @ApiOperation("查询所有后台资源")
    @GetMapping("/listAll")
    public List<Resource> listAll() {
        return resourceService.list();
    }

}
